package com.wanhelibs.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanhe.base.OnDialogFragmentDismissListener;
import com.wanhe.base.model.base.SetMealDish;
import com.wanhelibs.R;
import com.wanhelibs.custom_view.views.ChangeSetMealDialog;

import java.util.List;

/**
 * Created by XieZaiWei on 2017/8/10.
 * 加套餐Dialog Adapter
 */

public class AddSetMealAdapter extends BaseAdapter implements OnDialogFragmentDismissListener{
	private Context			mContext;
	private LayoutInflater	mInflater;
	private List <SetMealDish>		mArrDish;

	public AddSetMealAdapter(Context context, List <SetMealDish> arrDish) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mArrDish = arrDish;
	}

	@Override
	public int getCount() {
		if (mArrDish != null) {
			return mArrDish.size();
		} else {
			return 0;
		}

	}

	@Override
	public SetMealDish getItem(int position) {
		return mArrDish.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.add_set_meal_item, null);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.tv_change = (TextView) convertView.findViewById(R.id.tv_change);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final SetMealDish setMealDish = mArrDish.get(position);
		viewHolder.tv_name.setText(setMealDish.currentDish.getVch_dishname());
		if(setMealDish.childDish != null && setMealDish.childDish.size() > 1){
			viewHolder.tv_change.setText("换菜");
			viewHolder.tv_change.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					ChangeSetMealDialog changeSetMealDialog = ChangeSetMealDialog.newInstance();
					changeSetMealDialog.setSetMealDish(setMealDish);
					changeSetMealDialog.setOnDismissListener(AddSetMealAdapter.this);
					changeSetMealDialog.show(((FragmentActivity) mContext).getSupportFragmentManager(),
							"ChangeSetMealDialog");

				}
			});
		}

		return convertView;
	}

	private static class ViewHolder {
		public TextView	tv_name;
		public TextView	tv_change;
	}

	@Override
	public void onDialogFragmentDismiss(DialogInterface dialog) {
		notifyDataSetChanged();
	}

}
